package com.xc.freeapp.account.controller;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.account.controller.AbstractController;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Doctor;
import com.xc.freeapp.entity.DoctorIsCollect;
import com.xc.freeapp.entity.Messages;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.DoctorService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;
@Controller
@RequestMapping("/v3/specialist")
@Api(value="/v3/specialist",description = "诊所专家v3")
public class DoctorControllerV3 extends AbstractController{
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private DoctorService doctorservice;
	/**
	 * 好医生列表
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "专家列表")
	public ResponsePage<List<DoctorIsCollect>>  getDorctorByDept(
					@RequestParam(value = "page", required = false, defaultValue="0") Integer page,
					@RequestParam(value = "limit", required = false, defaultValue="15") Integer limit
			)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		
		log.info("好医生" + authInfo.getAppId() + "的医院获取列表");
		PageBounds pageBounds = new PageBounds(page, limit);
		List<DoctorIsCollect> doctorlist = doctorservice
				.getdoctorbyappid( authInfo.getAppId(),authInfo.getUserId(),pageBounds);
		for (DoctorIsCollect doctor2 : doctorlist) {
			doctor2.setHeadpic(UploadUtil.urlConversion(doctor2.getHeadpic()));
		}
		Paginator paginator =((PageList)doctorlist).getPaginator();
		return new ResponsePage<List<DoctorIsCollect>>(doctorlist, paginator);
	}
	/**
	 * 好医生搜索列表
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/bykeyword", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "专家搜索列表")
	public List<Doctor> getdoctorbykeywordS(@RequestParam("keyword") String keyword)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, keyword,
				"keyword");
		log.info("好医生" +authInfo.getAppId()+ "的医院搜索列表");
		List<Doctor> doctorlist = doctorservice.getdoctorbykeyword(keyword,
				authInfo.getAppId());
		for (Doctor doctor2 : doctorlist) {
			doctor2.setHeadpic(UploadUtil.urlConversion(doctor2.getHeadpic()));
		}
		return doctorlist;
	}
	/**
	 * 查询医生以及排班信息
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "{doctorid}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询医生以及排班信息")
	public Map<String, Object> getDoctorWork(@PathVariable("doctorid") String doctorid)
			throws BaseException, ParseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		
		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, doctorid,
				"doctorid");
		Map<String, Object> map = doctorservice.getWorkTimeListV2(doctorid,
				authInfo.getUserId());
		
		return map;
	}
	
	/**
	 * 好医生搜索列表
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/bydeptname", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "好医生搜索列表根据科室名字")
	public List<Doctor> getdoctorbyDeptName(@RequestParam("deptname") String deptname)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		PageBounds pageBounds = new PageBounds(0, 30);
		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, deptname,
				"keyword");
		log.info("好医生" +authInfo.getAppId()+ "的医院搜索列表");
		List<Doctor> doctorlist = doctorservice.getdoctorbyDeptName(deptname,
				authInfo.getAppId(),pageBounds);
		for (Doctor doctor2 : doctorlist) {
			doctor2.setHeadpic(UploadUtil.urlConversion(doctor2.getHeadpic()));
		}
		return doctorlist;
	}
	
	/**
	 * 好医生搜索列表
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/byspeciality", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "好医生搜索列表根据特长")
	public List<Doctor> getdoctorbyspeciality(@RequestParam("speciality") String speciality)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		PageBounds pageBounds = new PageBounds(0, 30);
		log.info("好医生" +authInfo.getAppId()+ "的医院搜索列表");
		List<Doctor> doctorlist = doctorservice.getdoctorbySpeciality(speciality,
				authInfo.getAppId(),pageBounds);
		for (Doctor doctor2 : doctorlist) {
			doctor2.setHeadpic(UploadUtil.urlConversion(doctor2.getHeadpic()));
		}
		return doctorlist;
	}
}
